package com.luobo.behavior.observer;


import java.util.ArrayList;
import java.util.List;

/**
 * 被观察者
 * 实现了Subject接口，对Subject接口的三个方法进行了具体实现
 */
public class ConcreteSubject implements Subject {


    //注意到这个List集合的泛型参数为Observer接口，设计原则：面向接口编程而不是面向实现编程
    private List<Observer> list;
    private String message;

    public ConcreteSubject() {
        list = new ArrayList<Observer>();
    }

    @Override
    public void addObserver(Observer observer) {
        list.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        list.remove(observer);
    }

    @Override
    public void notifyObserver() {
        for (Observer observer : list) {
            observer.update(message);
        }
    }


    public void send(String message) {
        System.out.println("被观察者更新消息:" + message);
        this.message = message;
        notifyObserver();
    }
}
